﻿using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WinServer
{
    //默认访问的hub名是首字母变小写的名称，如果没有HubName的注解的话，这个hub的名是myHub
    //[HubName("DataHub")]
    public partial class MyHub : Hub
    {
        public override Task OnConnected()
        {
            var param = Context.QueryString.Get("Param");
            if (param != null)
            {
                Groups.Add(Context.ConnectionId, "Client");
                Program.frmServer.WriteLog($"客户端ID连接成功：{Context.ConnectionId}");

                //var hub = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
                //Clients.All.SayHello("你好，连接成功，请说出你的指令：");
                Clients.Client(Context.ConnectionId).SayHello("你好，连接成功，请说出你的指令：");
                return base.OnConnected();
            }
            return Task.FromResult(0);
        }

        public override Task OnDisconnected(bool stopCalled)
        {
            Program.frmServer.WriteLog($"客户端ID关闭：{Context.ConnectionId}");
            return base.OnDisconnected(stopCalled);
        }

        public override Task OnReconnected()
        {
            Program.frmServer.WriteLog($"客户端ID重连：{Context.ConnectionId}");
            return base.OnReconnected();
        }
       
    }
}
